package com.teaching.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.teaching.Common.R;
import com.teaching.Common.Utils;
import com.teaching.Entity.Orde;
import com.teaching.Service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 获取用户可重课程信息
     *
     * @param data
     * @return
     */
    @PostMapping("/myclasses")
    public R<List<HashMap>> UserClass(@RequestBody HashMap<String, Object> data) {
        String state = Utils.getString(data,"state");
        String fun = Utils.getString(data,"fun");
        LambdaQueryWrapper<Orde> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(state), Orde::getClassState, state);
        if (fun.equals("Asc")) {
            queryWrapper.orderByAsc(Orde::getClassState);
        } else {
            queryWrapper.orderByDesc(Orde::getClassState);
        }
        List<Orde> list = orderService.list(queryWrapper);
        List<HashMap> hashMaps = new ArrayList<>();
        list.forEach(order -> {
            HashMap<String, Object> HashMap = new HashMap<>();
            HashMap.put("teacherName", order.getTeacherName());
            HashMap.put("className", order.getClassName());
            HashMap.put("classTime", Utils.TimeStampTurnString(order.getClassTime()));
            HashMap.put("classState",order.getClassState());
            hashMaps.add(HashMap);
        });
        return R.success(hashMaps);
    }
}
